/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_announce_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleAnnounceStub::SleAnnounceStub()
{
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_REGISTER_ANNOUNCE_CALLBACK] =
        &SleAnnounceStub::RegisterAnnounceCallbackInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_UNREGISTER_ANNOUNCE_CALLBACK] =
        &SleAnnounceStub::UnregisterAnnounceCallbackInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_CREATE_ANNOUNCE_ID] =
        &SleAnnounceStub::CreateAnnounceIdInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_REMOVE_ANNOUNCE_ID] =
        &SleAnnounceStub::RemoveAnnounceIdInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_ANNOUNCE_ENABLE] =
        &SleAnnounceStub::StartAnnounceInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_ANNOUNCE_DISABLE] =
        &SleAnnounceStub::StopAnnounceInner;
    memberFuncMap_[SleAnnounceInterfaceCode::SLE_GET_DEFAULT_ANNOUNCE_CONFIG] =
            &SleAnnounceStub::GetDefaultAnnounceParamConfigInner;
}

SleAnnounceStub::~SleAnnounceStub(){}

int SleAnnounceStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    std::u16string descriptor = SleAnnounceStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleAnnounceInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleAnnounceStub::RegisterAnnounceCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("RegisterAnnounceCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read announce remote failed!");
    const sptr<ISleAnnounceCallback> callback = OHOS::iface_cast<ISleAnnounceCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read announce callback failed!");

    int32_t result = RegisterAnnounceCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleAnnounceStub::UnregisterAnnounceCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("UnregisterAnnounceCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read announce remote failed!");
    const sptr<ISleAnnounceCallback> callback = OHOS::iface_cast<ISleAnnounceCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read announce callback failed!");

    int32_t result = UnregisterAnnounceCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleAnnounceStub::CreateAnnounceIdInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("CreateAnnounceIdInner In");
    uint8_t id;
    int32_t result = CreateAnnounceId(id);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(id), TRANSACTION_ERR, "write announce id failed!");
    return NO_ERROR;
}

int SleAnnounceStub::RemoveAnnounceIdInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("RemoveAnnounceIdInner In");
    uint8_t id;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(id), TRANSACTION_ERR, "read announce id failed!");

    int32_t result = RemoveAnnounceId(id);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleAnnounceStub::StartAnnounceInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("StartAnnounceInner In");
    uint8_t id;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(id), TRANSACTION_ERR, "read announce id failed!");

    std::shared_ptr<AnnounceData> datas(data.ReadParcelable<AnnounceData>());
    SLE_ASSERT_AND_RETURN_RET_LOG(datas, TRANSACTION_ERR, "read announce data failed!");

    std::shared_ptr<AnnounceParam> params(data.ReadParcelable<AnnounceParam>());
    SLE_ASSERT_AND_RETURN_RET_LOG(params, TRANSACTION_ERR, "read announce param failed!");

    int32_t result = StartAnnounce(id, *datas, *params);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleAnnounceStub::StopAnnounceInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("StartAnnounceInner In");
    uint8_t id;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(id), TRANSACTION_ERR, "read announce id failed!");
    int32_t result = StopAnnounce(id);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleAnnounceStub::GetDefaultAnnounceParamConfigInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetDefaultAnnounceParamConfigInner In");
    SleAnnounceParam announceParam;
    int32_t result = GetDefaultAnnounceParamConfig(announceParam);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(announceParam.handle), TRANSACTION_ERR, "write handle failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(announceParam.mode), TRANSACTION_ERR, "write mode failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(announceParam.role), TRANSACTION_ERR, "write role failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(announceParam.level), TRANSACTION_ERR, "write level failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint32(announceParam.annonceIntervalMin), TRANSACTION_ERR, "write annonceIntervalMin failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint32(announceParam.annonceIntervalMax), TRANSACTION_ERR, "write annonceIntervalMax failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(announceParam.channelMap), TRANSACTION_ERR, "write channelMap failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt8(announceParam.txPower), TRANSACTION_ERR, "write txPower failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(announceParam.connectIntervalMin), TRANSACTION_ERR, "write connectIntervalMin failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(announceParam.connectIntervalMax), TRANSACTION_ERR, "write connectIntervalMax failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(announceParam.connectLatency), TRANSACTION_ERR, "write connectLatency failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(announceParam.connectTimeout), TRANSACTION_ERR, "write connectTimeout failed!");
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS